package lumis.service.wsrp.consumer;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import lumis.portal.PortalRequestParameters;
import lumis.portal.serviceinterface.IServiceInterfaceActionRequest;

import com.sun.portal.container.ChannelMode;
import com.sun.portal.container.ChannelState;
import com.sun.portal.container.ChannelURLType;
import com.sun.portal.wsrp.common.WSRPSpecKeys;
import com.sun.portal.wsrp.consumer.markup.WSRPToContainerMap;
import com.sun.portal.wsrp.consumer.wsrpinvoker.WSRPWindowRequestReader;

/**
 * Provides information about a remote portlet request.
 *
 * @version $Revision: 11180 $ $Date: 2009-12-04 18:13:02 -0200 (Fri, 04 Dec 2009) $
 * @since 5.5.0
 */
class RemotePortletWindowRequestReader extends WSRPWindowRequestReader
{
	private final IServiceInterfaceActionRequest interfaceRequest;

	public RemotePortletWindowRequestReader(IServiceInterfaceActionRequest interfaceRequest)
	{
		this.interfaceRequest = interfaceRequest;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String[]> readParameterMap(HttpServletRequest request)
	{
		Map<String, String[]> result = new HashMap<String, String[]>();
		
		String[] navigationalState = interfaceRequest.getParameterValues(WSRPChannelURLFactory.PARAMETER_NAVIGATIONAL_STATE);
		if (navigationalState != null)
			result.put(WSRPSpecKeys.NAVIGATIONAL_STATE, navigationalState);
		
		String[] interactionState = interfaceRequest.getParameterValues(WSRPChannelURLFactory.PARAMETER_INTERACTION_STATE);
		if (interactionState != null)
			result.put(WSRPSpecKeys.INTERACTION_STATE, interactionState);
		
		// add all non-reserved request parameters to result (usually form POST data)
		for (Object entryObject: interfaceRequest.getParameterMap().entrySet())
		{
			Map.Entry<String, String[]> entry = (Entry<String, String[]>) entryObject;
			String parameterName = entry.getKey();
			if (!WSRPChannelURLFactory.isReservedParameter(parameterName) && !PortalRequestParameters.isReservedParameter(parameterName))
				result.put(parameterName, entry.getValue());
		}
		
		return result;
	}

	@Override
	public String getCacheLevel(HttpServletRequest request)
	{
		return interfaceRequest.getParameter(WSRPChannelURLFactory.PARAMETER_RESOURCE_CACHE_LEVEL);
	}

	@Override
	public String getResourceID(HttpServletRequest request)
	{
		return interfaceRequest.getParameter(WSRPChannelURLFactory.PARAMETER_RESOURCE_ID);
	}

	@Override
	public ChannelMode readNewPortletWindowMode(HttpServletRequest request)
	{
		String portletModeParam = interfaceRequest.getParameter(WSRPChannelURLFactory.PARAMETER_PORTLET_MODE);
		if (portletModeParam != null && portletModeParam.length() > 0)
			return WSRPToContainerMap.mapChannelModeToContainer(portletModeParam);
		else
			return null;
	}

	@Override
	public ChannelState readNewWindowState(HttpServletRequest request)
	{
		String windowStateParam = interfaceRequest.getParameter(WSRPChannelURLFactory.PARAMETER_WINDOW_STATE);
		if (windowStateParam != null && windowStateParam.length() > 0)
			return WSRPToContainerMap.mapWindowStateToContainer(windowStateParam);
		else
			return null;
	}

	@Override
	public ChannelURLType readURLType(HttpServletRequest request)
	{
		String urlType = interfaceRequest.getParameter(WSRPChannelURLFactory.PARAMETER_URL_TYPE);
		if (urlType != null)
		{
			return WSRPToContainerMap.mapURLTypeToContainer(urlType);
		}
		else
		{
			throw new RuntimeException("Bad WSRP URL");
		}
	}
}